package org.mx.dal.hibernate.entity;

import javax.persistence.*;

import org.mx.dal.entity.BaseDictTree;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Set;

/**
 * 基于Hibernate实现的基础树状字典实体
 * 
 * @param <T> 泛型定义
 *
 * @author : john.peng date : 2017/10/6
 * @see BaseDictEntity
 * @see BaseDictTree
 */
@MappedSuperclass
@ToString(callSuper = true, exclude = { "children" })
public class BaseDictTreeEntity<T extends BaseDictTree<T>> extends BaseDictEntity implements BaseDictTree<T> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8264565447069110185L;
	@Column(name = "LIKED_CODE", nullable = false, unique = true, length = 255)
    private @Getter @Setter String likedCode;
	@ManyToOne(cascade = { CascadeType.ALL })
	@JoinColumn(name = "PARENT_ID")
	private T parent;
	@OneToMany(cascade = { CascadeType.REFRESH }, fetch = FetchType.LAZY)
	@JoinColumn(name = "PARENT_ID")
	private Set<T> children;

	/**
	 * {@inheritDoc}
	 *
	 * @see BaseDictTree#getParentId()
	 */
	@Override
	public String getParentId() {
		T parent = getParent();
		return parent == null ? null : parent.getId();
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see BaseDictTree#getParent()
	 */
	@Override
	public T getParent() {
		return parent;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see BaseDictTree#setParent(BaseDictTree)
	 */
	@Override
	public void setParent(T parent) {
		this.parent = (T) parent;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see BaseDictTree#getChildren()
	 */
	@Override
	public Set<T> getChildren() {
		return children;
	}
}
